/*
 * @Author: your name
 * @Date: 2021-05-19 17:47:55
 * @LastEditTime: 2021-05-29 16:39:59
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /xiangpan-admin/src/pages/staff/index.tsx
 */
import { IForm } from '@/components/form/interface';
import {
  Button,
  Card,
  Space,
  Table,
  Tag,
  Modal,
  TablePaginationConfig,
} from 'antd';
import React, { useState } from 'react';
import ProjectModal from './projectModal';
import './index.less';
import { PlusOutlined } from '@ant-design/icons';
import useObj from '@/use/useObj';
import initPagination from '@/use/initPagination';
import { globalModel } from '../globalModel';
import HeaderForm from '@/components/form';
import { IFormTypeEnum } from '@/components/form/formComponent';
import { EPermission } from '@/enum';
import { useRequest } from 'umi';
import { delete_project, get_project_list } from '@/services/pages';
import { formatDateTime } from '@/utils/utils';
import { useEffect } from 'react';

interface IProps {}

const Project = (props: IProps) => {
  const [obj, setObj] = useObj({
    ...initPagination,
    search: null,
    visible: false,
    refresh: null,
    row: null,
  });

  const { data, error, loading } = useRequest(
    () =>
      get_project_list({
        page_size: obj.pagination.pageSize,
        page_num: obj.pagination.current,
        search: obj.search,
      }),
    {
      refreshDeps: [obj.pagination, obj.refresh, obj.search],
    },
  );

  const columns: IForm[] = [
    {
      key: 'project_name',
      dataIndex: 'project_name',
      title: '项目名称',
    },
    {
      key: 'origin_price',
      dataIndex: 'origin_price',
      title: '项目价格',
      render: (text) => {
        return <span>{text / 100}</span>;
      },
    },
    {
      key: 'vip_one',
      dataIndex: 'vip_one',
      title: '黄金会员',
      render: (text) => {
        return <span>{text / 100}</span>;
      },
    },
    {
      key: 'vip_two',
      dataIndex: 'vip_two',
      title: '铂金会员',
      render: (text) => {
        return <span>{text / 100}</span>;
      },
    },
    {
      key: 'vip_three',
      dataIndex: 'vip_three',
      title: '钻石会员',
      render: (text) => {
        return <span>{text / 100}</span>;
      },
    },
    {
      key: 'staff_price',
      dataIndex: 'staff_price',
      title: '员工提成',
      render: (text) => {
        return <span>{text / 100}</span>;
      },
    },
    {
      key: 'during',
      dataIndex: 'during',
      title: '项目时长(分钟)',
      render: (text, record) => {
        return text / 60;
      },
    },
    {
      key: 'create_time',
      dataIndex: 'create_time',
      title: '创建时间',
      render: (text, record) => {
        return formatDateTime(text);
      },
    },
    {
      key: 'opt',
      dataIndex: 'opt',
      title: '操作',
      render: (text, record) => {
        return (
          <Space>
            <Button
              type="primary"
              onClick={() => setObj({ row: record, visible: true })}
            >
              编辑
            </Button>
            <Button
              type="primary"
              danger
              onClick={() => {
                deleteProject(record.project_id);
              }}
            >
              删除
            </Button>
          </Space>
        );
      },
    },
  ];

  const deleteProject = (project_id: number) => {
    Modal.confirm({
      title: '删除操作！',
      content: '你确定要删除该条数据？',
      onOk: async () => {
        await delete_project({ project_id: project_id });
        setObj((obj) => {
          obj.pagination.current = 1;
          obj.refresh = Symbol();
        });
      },
    });
  };

  const List = [
    {
      key: 'search',
      type: IFormTypeEnum.Input,
      label: '项目',
    },
  ];

  useEffect(() => {
    setObj((obj) => {
      obj.pagination.total = data?.results[0]?.total || 0;
    });
  }, [data]);
  return (
    <Card title="项目管理" className="project">
      <div className="flex-between">
        <HeaderForm
          list={List}
          layout="inline"
          style={{ margin: '10px 0' }}
          onSubmit={(values) => {
            setObj({ search: values.search });
          }}
        />
        {/* {globalModel.userInfo?.is_super === EPermission.StoreAdmin && ( */}
        <Button
          type="primary"
          icon={<PlusOutlined />}
          style={{ marginBottom: 15 }}
          onClick={() => {
            setObj({ visible: true, row: null });
          }}
        >
          添加项目
        </Button>
        {/* )} */}
      </div>

      <Table
        rowKey={(record) => record.project_id}
        loading={loading}
        columns={columns}
        dataSource={data?.results}
        pagination={obj.pagination}
        onChange={(pagination: TablePaginationConfig): void => {
          const { current, pageSize } = pagination;
          setObj((obj: any) => {
            obj.pagination.current = current;
            obj.pagination.pageSize = pageSize;
            obj.onReFresh = Symbol();
          });
        }}
      />
      {obj.visible && (
        <ProjectModal
          row={obj.row}
          visible={obj.visible}
          close={() => setObj({ visible: false })}
          refresh={() => {
            setObj((obj) => {
              obj.pagination.current = 1;
              obj.refresh = Symbol();
            });
          }}
        />
      )}
    </Card>
  );
};
export default Project;
